#pragma once
//xml
/*
config
	proxy abbribute on
		ip
		port
		name
		pwd
	proxy
config
*/

#include "..\common\XmlUtil.h"
#include <shlwapi.h>

class CDataManger
{
public:
	CDataManger();
	~CDataManger();
	static std::string isProxyOn;
	static std::string ip;
	static std::string port;
	static std::string name;
	static std::string pwd;

};

#define NOOB_CONFIG_XML "noob_config.xml"
using namespace std;
using namespace linktong::util;


inline void ReadXmlConfig()
{
	try{
		CXmlUtil  xml;
		TiXmlDocument document ; 
		xml.CreateDocumentFromFile(document, NOOB_CONFIG_XML);  //information from file
		TiXmlElement * configElement = xml.GetRootElement(document);//config,root
		if(configElement){
			TiXmlElement * proxyElement = xml.GetChildElementByTagName(configElement,"proxy");
			if(proxyElement){
				string isOn;
				if(xml.GetElementAttributeValue(proxyElement,"on",isOn)){
					CDataManger::isProxyOn = isOn;
				}
				std::string _;
				if(TiXmlElement* ipElement = xml.GetChildElementByTagName(proxyElement,"ip")){//ip
					if(xml.GetElementValue(ipElement,_)){
						CDataManger::ip = _;
					}
				}
				if(TiXmlElement* portElement = xml.GetChildElementByTagName(proxyElement,"port")){//port
					if(xml.GetElementValue(portElement,_)){
						CDataManger::port = _;
					}
				}
				if(TiXmlElement* nameElement = xml.GetChildElementByTagName(proxyElement,"name")){//name
					if(xml.GetElementValue(nameElement,_)){
						CDataManger::name = _;
					}
				}
				if(TiXmlElement* pwdElement = xml.GetChildElementByTagName(proxyElement,"pwd")){//password
					if(xml.GetElementValue(pwdElement,_)){
						CDataManger::pwd = _;
					}
				}
			}
		}	
	}catch (...){

	}
}

inline void WriteXmlConfig(const string& on)
{
	try{
		CXmlUtil  xml;
		xml.AddXmlDeclaration("1.0","utf-8","");
		if(TiXmlElement* root=xml.AddXmlRootElement("config")){//root
			if(TiXmlElement* pProxy = xml.AddXmlChildElement(root,"proxy")){
				CDataManger::isProxyOn = on;
				xml.AddXmlAttribute(pProxy,"on",on.c_str());
				if(TiXmlElement* _ = xml.AddXmlChildElement(pProxy,"ip")){
					xml.AddElementValue(_,CDataManger::ip.c_str());
				}
				if(TiXmlElement* _ = xml.AddXmlChildElement(pProxy,"port")){
					xml.AddElementValue(_,CDataManger::port.c_str());
				}
				if(TiXmlElement* _ = xml.AddXmlChildElement(pProxy,"name")){
					xml.AddElementValue(_,CDataManger::name.c_str());
				}
				if(TiXmlElement* _ = xml.AddXmlChildElement(pProxy,"pwd")){
					xml.AddElementValue(_,CDataManger::pwd.c_str());
				}
				xml.SaveFile(NOOB_CONFIG_XML);//saving file
			}
		}
	} catch(...) {

	}
}
